//
//  MapHierarchyView.m
//  location
//
//  Created by Dongxiang Xu on 2024/4/16.
//

#import "MapHierarchyView.h"

@interface MapHierarchyView ()

@property (nonatomic, strong) UIView *infoView;
@property (nonatomic, strong) UIView *roadView;

@end

@implementation MapHierarchyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    UIButton *bgButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self addSubview:bgButton];
    [[bgButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self singleTap];
    }];
    
    _infoView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self addSubview:_infoView];
    
    CALayer *infoLayer = [CALayer layer];
    infoLayer.frame = _infoView.bounds;
    infoLayer.cornerRadius = 25;
    infoLayer.masksToBounds = NO;
    infoLayer.backgroundColor = RealWhiteColor.CGColor;
    infoLayer.shadowColor = RealBlackColor.CGColor;
    infoLayer.shadowOffset = CGSizeMake(0,2);
    infoLayer.shadowOpacity = 0.3;
    infoLayer.shadowRadius = 3;
    [_infoView.layer addSublayer:infoLayer];
    
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(_infoView.width-48, 18, 30, 30)];
    [close setImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
    [_infoView addSubview:close];
    [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self singleTap];
    }];
    
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH-40, 30)];
    typeLabel.font = MEDIUM_FONT(16);
    typeLabel.textColor = RealBlackColor;
    typeLabel.text = @"地图类型";
    [self.infoView addSubview:typeLabel];

    NSInteger mapType = [[[NSUserDefaults standardUserDefaults] objectForKey:MapType] integerValue];
    
    NSArray *typeArray = @[@{@"title":@"街道",@"img":@"map_normal"},@{@"title":@"卫星",@"img":@"map_black"},@{@"title":@"智能切换",@"img":@"map_auto"}];
    for (int i = 0; i < typeArray.count; i ++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20+i*((SCREEN_WIDTH-60)/3+10), CGRectGetMaxY(typeLabel.frame)+28, (SCREEN_WIDTH-60)/3, (SCREEN_WIDTH-60)/3*0.702)];
        view.tag = 200+i;
        [self.infoView addSubview:view];
        
        CALayer *layer = [CALayer layer];
        layer.frame = view.bounds;
        layer.cornerRadius = 8;
        layer.masksToBounds = NO;
        layer.backgroundColor = RealWhiteColor.CGColor;
        layer.shadowColor = RealBlackColor.CGColor;
        layer.shadowOffset = CGSizeMake(0,2);
        layer.shadowOpacity = 0.3;
        layer.shadowRadius = 3;
        [view.layer addSublayer:layer];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20+i*((SCREEN_WIDTH-60)/3+10), CGRectGetMaxY(typeLabel.frame)+28, (SCREEN_WIDTH-60)/3, (SCREEN_WIDTH-60)/3*0.702)];
        button.tag = i+50;
        button.layer.masksToBounds = YES;
        [button.layer setBorderWidth:2];
        [button.layer setCornerRadius:8];
        [self.infoView addSubview:button];
        
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, button.width, button.height)];
        img.image = [UIImage imageNamed:typeArray[i][@"img"]];
        [button addSubview:img];
        
        if (i == mapType) {
            [button.layer setBorderColor:NormalMainColor.CGColor];
            view.hidden = NO;
        }else{
            [button.layer setBorderColor:[UIColor clearColor].CGColor];
            view.hidden = YES;
        }
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            for (int i = 0; i < typeArray.count; i ++) {
                [[self viewWithTag:50+i].layer setBorderColor:[UIColor clearColor].CGColor];
                [self viewWithTag:200+i].hidden = YES;
            }
            self.roadView.hidden = (i==0)?YES:NO;
            [button.layer setBorderColor:NormalMainColor.CGColor];
            view.hidden = NO;
            
            
            [[NSUserDefaults standardUserDefaults] setValue:@(i) forKey:MapType];
            [[NSUserDefaults standardUserDefaults] synchronize];
            if (self.MapHierarchyViewBlock) {
                self.MapHierarchyViewBlock(i);
            }
        }];
    }
    
    UILabel *traffic = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY([self viewWithTag:50].frame)+32, 120, 26)];
    traffic.font = REAL_MEDIUM_FONT(15);
    traffic.textColor = RealBlackColor;
    traffic.text = @"显示交通状况";
    [self.infoView addSubview:traffic];
    
    UISwitch *trafficSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-68, traffic.y, 48, 26)];
    [trafficSwitch setFrame:CGRectMake(SCREEN_WIDTH-trafficSwitch.width-20, traffic.y+(traffic.height-trafficSwitch.height)/2, trafficSwitch.width, trafficSwitch.height)];
    trafficSwitch.onTintColor = NormalMainColor;
    trafficSwitch.tintColor = RealWhiteColor;
    trafficSwitch.tag = 100;
    [trafficSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    BOOL showTraffic = [[[NSUserDefaults standardUserDefaults] objectForKey:ShowTraffic] boolValue];
    [trafficSwitch setOn:showTraffic animated:NO];
    [self.infoView addSubview:trafficSwitch];
    
    _roadView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(traffic.frame)+32, SCREEN_WIDTH, 26)];
    self.roadView.hidden = (mapType==0)?YES:NO;
    [self.infoView addSubview:_roadView];
    
    UILabel *road = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 120, _roadView.height)];
    road.font = REAL_MEDIUM_FONT(15);
    road.textColor = RealBlackColor;
    road.text = @"显示路网标签";
    [self.roadView addSubview:road];
    
    UISwitch *roadSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-68, road.y, 48, _roadView.height)];
    [roadSwitch setFrame:CGRectMake(SCREEN_WIDTH-roadSwitch.width-20, road.y+(road.height-roadSwitch.height)/2, roadSwitch.width, roadSwitch.height)];
    roadSwitch.onTintColor = NormalMainColor;
    roadSwitch.tintColor = RealWhiteColor;
    roadSwitch.tag = 101;
    [roadSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    BOOL roadNet = [[[NSUserDefaults standardUserDefaults] objectForKey:RoadNet] boolValue];
    [roadSwitch setOn:roadNet animated:NO];
    [self.roadView addSubview:roadSwitch];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.infoView setFrame:CGRectMake(0, SCREEN_HEIGHT-370, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }];
}

- (void)switchAction:(UISwitch *)sender
{
    if (sender.tag==100) {
        [[NSUserDefaults standardUserDefaults] setValue:@(sender.on) forKey:ShowTraffic];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else if (sender.tag==101){
        [[NSUserDefaults standardUserDefaults] setValue:@(sender.on) forKey:RoadNet];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    NSInteger mapType = [[[NSUserDefaults standardUserDefaults] objectForKey:MapType] integerValue];
    if (self.MapHierarchyViewBlock) {
        self.MapHierarchyViewBlock(mapType);
    }
}

- (void)singleTap
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.infoView setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }completion:^(BOOL finished) {
        [self removeFromSuperview];        
    }];
    if (self.MapHierarchyDismissBlock) {
        self.MapHierarchyDismissBlock();
    }
}

@end
